package com.zhang.zhangapp.view.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhang.zhangapp.R;
import com.zhang.zhangapp.base.http.MCallback;
import com.zhang.zhangapp.bean.user.Bill;
import com.zhang.zhangapp.bean.user.BillItem;
import com.zhang.zhangapp.http.CommonHttp;
import com.zhang.zhangapp.utils.DensityUtils;
import com.zhang.zhangapp.view.BaseAppCompatActivityActivity;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/25.
 */

@ContentView(R.layout.activity_bill_details)
public class BillDetailActivity extends BaseAppCompatActivityActivity {

    @ViewInject(R.id.type_image)
    ImageView typeImageView;
    @ViewInject(R.id.action_bar_title)
    TextView titleText;
    @ViewInject(R.id.title_text)
    TextView pricetitleText;
    @ViewInject(R.id.price_type_text)
    TextView priceTypeText;
    @ViewInject(R.id.type_desc_text)
    TextView typeDescTxt;
    @ViewInject(R.id.desc_layout1)
    LinearLayout descLayout1;
    private int billId;
    Bill detailObj;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        billId = getIntent().getIntExtra("billId", 0);
        x.view().inject(this);
        titleText.setText("账单详情");
        getBillDetail();
    }

    @Event(type = View.OnClickListener.class, value = R.id.action_bar_left_image)
    private void back(View view) {
        this.finish();
    }

    private void initView() {
        if (detailObj != null) {
            BillItem extData = detailObj.getExtData();
            try {
                if ("1".equals(detailObj.getTransType())) {
                    typeImageView.setImageResource(R.mipmap.recharge);
                } else {
                    typeImageView.setImageResource(R.mipmap.xiaofei);
                }
                pricetitleText.setText(detailObj.getTitle());
                typeDescTxt.setText(extData.getTransResult());
                priceTypeText.setText(detailObj.getAmount() + "");
                addDesLayout("订单金额~" + extData.getRealAmount());
                String[] arr = extData.getProList().split(",");

                for (String str : arr) {
                    addDesLayout(str);
                }
            } catch (Exception e) {
            }
        }
    }

    private void addDesLayout(String str) {
        int padding = DensityUtils.dp2px(this, 8);
        LinearLayout layout = new LinearLayout(this);
        layout.setPadding(padding, padding, padding, padding);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        TextView textView = new TextView(this);
        textView.setText(str.split("~")[0]);
        textView.setTextColor(this.getResources().getColor(R.color.black_333));
        TextView valueText = new TextView(this);
        valueText.setText(str.split("~")[1]);
        valueText.setGravity(Gravity.RIGHT);
        valueText.setTextColor(this.getResources().getColor(R.color.black_333));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        valueText.setLayoutParams(layoutParams);
        layout.addView(textView);
        layout.addView(valueText);
        descLayout1.addView(layout);
    }
//    <LinearLayout
//    android:layout_width="match_parent"
//    android:layout_height="wrap_content"
//    android:paddingTop="@dimen/margin_x8"
//    android:paddingBottom="@dimen/margin_x8"
//    android:orientation="horizontal">
//
//                <TextView
//    android:layout_width="wrap_content"
//    android:layout_height="wrap_content"
//    android:text="订单金额"
//    android:textColor="@color/black_333" />
//
//                <TextView
//    android:id="@+id/order_amount"
//    android:text="150"
//    android:gravity="right"
//    android:layout_width="match_parent"
//    android:layout_height="wrap_content"
//    android:textColor="@color/black_333" />
//            </LinearLayout>

    private void getBillDetail() {
        if (showLoadingDialog()) {
            Map<String, String> params = new HashMap<>();
            params.put("transId", billId + "");
            CommonHttp.commonHttp("hzg/member/billDetail", params, new MCallback() {
                @Override
                public void onSuccess(String data) {
                    Gson gson = new Gson();
                    try {
                        detailObj = gson.fromJson(data, Bill.class);
                        initView();
                    } catch (Exception e) {
                    }
                }

                @Override
                public void onSuccess(String message, String code) {

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(Callback.CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    dismisionDalog();
                }
            });
        }
    }

}
